<template>
  <view class="register-container">
    <view class="register-box">
      <view class="title">用户注册</view>
      <view class="input-group">
        <input type="text" v-model="registerForm.username" placeholder="请输入用户名" />
      </view>
      <view class="input-group">
        <input type="password" v-model="registerForm.password" placeholder="请输入密码" />
      </view>
      <view class="input-group">
        <input type="text" v-model="registerForm.nickname" placeholder="请输入昵称" />
      </view>
      <view class="input-group">
        <input type="text" v-model="registerForm.email" placeholder="请输入邮箱" />
      </view>
      <view class="input-group">
        <input type="text" v-model="registerForm.phone" placeholder="请输入手机号" />
      </view>
      <view class="btn-group">
        <button class="register-btn" @click="handleRegister">注册</button>
        <button class="login-btn" @click="goToLogin">返回登录</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        nickname: '',
        email: '',
        phone: ''
      }
    }
  },
  methods: {
    async handleRegister() {
      // 表单验证
      if (!this.registerForm.username || !this.registerForm.password) {
        uni.showToast({
          title: '用户名和密码不能为空',
          icon: 'none'
        });
        return;
      }
      
      if (!this.validateEmail(this.registerForm.email)) {
        uni.showToast({
          title: '请输入正确的邮箱格式',
          icon: 'none'
        });
        return;
      }
      
      if (!this.validatePhone(this.registerForm.phone)) {
        uni.showToast({
          title: '请输入正确的手机号格式',
          icon: 'none'
        });
        return;
      }
      
      try {
        const res = await uni.request({
          url: 'http://localhost:8080/register',
          method: 'POST',
          data: this.registerForm,
          header: {
            'Content-Type': 'application/json'
          }
        });
        
        if (res[1].data.code === 2000) {
          uni.showToast({
            title: '注册成功',
            icon: 'success'
          });
          // 注册成功后跳转到登录页
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        } else {
          uni.showToast({
            title: res[1].data.msg || '注册失败',
            icon: 'none'
          });
        }
      } catch (e) {
        console.error(e);
        uni.showToast({
          title: '注册失败，请稍后重试',
          icon: 'none'
        });
      }
    },
    goToLogin() {
      uni.navigateBack();
    },
    validateEmail(email) {
      const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
      return reg.test(email);
    },
    validatePhone(phone) {
      const reg = /^1[3-9]\d{9}$/;
      return reg.test(phone);
    }
  }
}
</script>

<style lang="scss">
.register-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  
  .register-box {
    width: 80%;
    padding: 30rpx;
    background-color: #fff;
    border-radius: 20rpx;
    box-shadow: 0 0 20rpx rgba(0,0,0,0.1);
    
    .title {
      font-size: 40rpx;
      font-weight: bold;
      text-align: center;
      margin-bottom: 50rpx;
    }
    
    .input-group {
      margin-bottom: 30rpx;
      
      input {
        width: 100%;
        height: 80rpx;
        padding: 0 20rpx;
        border: 1px solid #ddd;
        border-radius: 10rpx;
        font-size: 28rpx;
      }
    }
    
    .btn-group {
      margin-top: 50rpx;
      
      button {
        width: 100%;
        height: 80rpx;
        line-height: 80rpx;
        text-align: center;
        border-radius: 10rpx;
        font-size: 32rpx;
        margin-bottom: 20rpx;
      }
      
      .register-btn {
        background-color: #007AFF;
        color: #fff;
      }
      
      .login-btn {
        background-color: #fff;
        color: #007AFF;
        border: 1px solid #007AFF;
      }
    }
  }
}
</style> 